{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "before\n",
      "before2\n",
      "hello\n",
      "after2\n",
      "after\n"
     ]
    }
   ],
   "source": [
    "#包\n",
    "#包就是文件夹，但该文件夹下必须存在__init__.py文件，该文件的内容可以为空。__init__.py用于标识当前文件夹是一个包。避免模块名冲突。\n",
    "#import pack_01\n",
    "#导入包\n",
    "\n",
    "# pack_01.__all__ #查看包中有哪些模块\n",
    "# pack_01.case01.test() #调用包中的模块中的函数\n",
    "# pack_01.case01.test2() #没有被__all__包含的函数，不能被调用 ！！！\n",
    "\n",
    "# from pack_01 import * #导入包中的所有模块\n",
    "# pack_01.case01.test() #调用包中的模块中的函数\n",
    "# pack_01.case01.test2() #没有被__all__包含的函数，不能被调用 ！！！\n",
    "# # 递归函数\n",
    "# # 阶乘定义为从 1 乘到该数的所有正整数的乘积\n",
    "# def fact(n):\n",
    "#     if n == 1:\n",
    "#         return 1\n",
    "#     else:\n",
    "#         return n * fact(n-1)\n",
    "# print(fact(5))\n",
    "# #斐波那契数列 1,1,2,3,5,8,13,21,34,55,89\n",
    "# def fact(n):\n",
    "#     if n <= 1:\n",
    "#         return 1\n",
    "#     else:\n",
    "#         return fact(n-2) + fact(n-1)\n",
    "# print(fact(7))\n",
    "# #闭包：函数嵌套函数，内部函数使用外部函数的变量，外部函数返回内部函数\n",
    "# def outer():\n",
    "#     a = 1\n",
    "#     def inner():\n",
    "#         print(a)\n",
    "#     return inner\n",
    "# outer()() #调用闭包函数\n",
    "# #闭包函数的作用：1.可以保存外部函数的局部变量，2.可以保存外部函数的参数，3.可以保存外部函数的返回值\n",
    "# def outer(a):\n",
    "#     def inner():\n",
    "#         print(a)\n",
    "#     return inner\n",
    "# outer(10)()\n",
    "# def outer(a):\n",
    "#     def inner(b):\n",
    "#         print(a,b)\n",
    "#     return inner\n",
    "# outer(10)(20)\n",
    "# #函数嵌套函数，内部函数使用外部函数的变量，外部函数返回内部函数\n",
    "# def outer(fn):\n",
    "#     def inner(*args,**kwargs):\n",
    "#         print('before')\n",
    "#         fn(*args,**kwargs)\n",
    "#         print('after')\n",
    "#     return inner\n",
    "# outer(print)('hello')\n",
    "# # 输出\n",
    "# # before\n",
    "# # hello\n",
    "# # after\n",
    "#闭包函数的应用：1.装饰器，2.偏函数\n",
    "def outer(fn):\n",
    "    def inner(*args,**kwargs):\n",
    "        print('before')\n",
    "        fn(*args,**kwargs)\n",
    "        print('after')\n",
    "    return inner\n",
    "def outer2(fn):\n",
    "    def inner(*args,**kwargs):\n",
    "        print('before2')\n",
    "        fn(*args,**kwargs)\n",
    "        print('after2')\n",
    "    return inner\n",
    "@outer\n",
    "@outer2 #装饰器 多个装饰器 从下往上执行\n",
    "def print_hello():\n",
    "    print('hello')\n",
    "print_hello()\n",
    "# # 输出\n",
    "# before\n",
    "# before2\n",
    "# hello\n",
    "# after2\n",
    "# after\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
